﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Properties;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins.ViewManagement;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Page
{
    public class Count : Pages, IParser, IMetaParser
    {
        public Count()
        {
            this.Options = "DirectDescendants";
        }

    
        [Property(Description = "Set an arbitrary break index to allow template customization at the item index")]
        public int? BreakIndex { get; set; }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            
            var currentPage = stack.OfType<MethodWorx.CMS.Content.Page>().FirstOrDefault();
            var pages = this.GetPages(context, currentPage);

            return pages.Count();
        }

        public string Name
        {
            get { return "Count";  }
        }

        public string Namespace
        {
            get { return "Page"; }
        }

        public string GetDescription()
        {
            return "Counts all matching pages from a set of filters";
        }

        public ParserMetaParameter[] GetParameters(Stack<object> stack, ParserMetaParameter[] parameters)
        {
            return parameters;
        }
    }
}
